package com.security.door.controller.accessManage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.baseUserRole;
import com.security.door.model.dto.baseUserRoleDto;
import com.security.door.service.IbaseUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

@RestController
@Api(value = "WEB - baseUserRoleController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class baseUserRoleController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IbaseUserRoleService ibaseUserRoleService;

    @GetMapping(value = "/userRole/getUsers/{roleCode}")
    @ApiOperation(value = "分页查询所有用户信息", notes = "分页查询所有用户信息")
    public Wrapper<PageInfo<baseUserRole>> queryListWithPage(@PathVariable("roleCode") String roleCode) {
        baseUserRoleDto entityDto = new baseUserRoleDto();
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        entityDto.setRolecode(roleCode);
        PageHelper.startPage(pageIndex, pageSize);
        var list = ibaseUserRoleService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @PostMapping(value = "/userRole/delRoleUsers")
    public Wrapper deleteRoleUsers(@RequestBody baseUserRole entity) {
        ibaseUserRoleService.delete(entity);
        return WrapMapper.ok("操作成功");
    }

    @PostMapping(value = "/userRole/addRoleUsers")
    public Wrapper addRoleUsers(@RequestBody baseUserRole entity) {
        if (!ibaseUserRoleService.hasRegister(entity)) {
            entity.setGuid(UUID.randomUUID().toString());
            entity.setCreatetime(new Date());
            ibaseUserRoleService.save(entity);
            return WrapMapper.ok("操作成功");
        }
        else
            return WrapMapper.error("已经添加");
    }
}
